package com.qztc.hire_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qztc.hire_end_core.mapper.HanFuSizeMapper;
import com.qztc.hire_end_dto.dto.HanFuSize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/hanfuSize")
@Tag(name = "汉服尺码管理接口")
public class HanFuSizeController {
    @Autowired
    private HanFuSizeMapper hanFuSizeMapper;

    @Operation(summary = "查询所有汉服尺码")
    @GetMapping("/getAll")
    public List<HanFuSize> getAll() {
        return new HanFuSize().selectAll();
    }

    @Operation(summary = "根据hanfuSizeId获取具体汉服尺码")
    @GetMapping("/getbyhanfuSizeId")
    public HanFuSize getbyhanfuSizeId(@RequestParam Integer hanFuSizeId) {
        QueryWrapper<HanFuSize> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hanfu_sizeid", hanFuSizeId);
        return new HanFuSize().selectOne(queryWrapper);
    }

    @Operation(summary = "根据hanfuSizeId删除某一个具体汉服尺码")
    @DeleteMapping("/delete")
    public boolean delete(@RequestBody HanFuSize hanFuSize) {
        return hanFuSize.deleteById(hanFuSize.getHanfuSizeId());
    }

    @Operation(summary = "根据hanfuSizeId修改某一个具体汉服尺码")
    @PutMapping("/update")
    public boolean update(@RequestBody HanFuSize hanFuSize) {
        QueryWrapper<HanFuSize> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hanfu_sizename", hanFuSize.getHanfuSizeName());
        if(hanFuSize.selectCount(queryWrapper) > 0) {
            return false;
        }
        return hanFuSize.updateById();
    }

    @Operation(summary = "增加汉服尺码")
    @PostMapping("/add")
    public boolean add(@RequestBody HanFuSize hanFuSize) {
        QueryWrapper<HanFuSize> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hanfu_sizename", hanFuSize.getHanfuSizeName());
        if(hanFuSize.selectCount(queryWrapper) > 0) {
            return false;
        }
        return hanFuSize.insert();
    }

    @Operation(summary = "批量删除汉服尺码")
    @DeleteMapping("/deleteMany")
    public boolean deleteMany(@RequestBody List<Integer> hanfuSizeIds) {
        if (hanfuSizeIds == null || hanfuSizeIds.isEmpty()) {
            return false;
        }
        QueryWrapper<HanFuSize> wrapper = new QueryWrapper<>();
        wrapper.in("hanfu_sizeid", hanfuSizeIds);
        return new HanFuSize().delete(wrapper);
    }

    @Operation(summary = "批量增加汉服尺码")
    @PostMapping("/addMany")
    public boolean addMany(@RequestBody List<HanFuSize> hanFuSizes) {
        if (hanFuSizes == null || hanFuSizes.isEmpty()) {
            return false;
        }
        // 批量重复校验
        QueryWrapper<HanFuSize> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("hanfu_sizename",
                hanFuSizes.stream().map(HanFuSize::getHanfuSizeName).collect(Collectors.toList()));
        if (hanFuSizeMapper.selectCount(queryWrapper) > 0) {
            return false;
        }
        // 批量插入SQL
        String sql = "insert into hanfu_size (hanfu_sizeid, hanfu_sizename) values" +
                hanFuSizes.stream()
                        .map(size -> String.format("(%d,'%s')", size.getHanfuSizeId(), size.getHanfuSizeName()))
                        .collect(Collectors.joining(","));
        return hanFuSizeMapper.executeSql(sql);
    }
}